//
//  HMNetworkTools.swift
//  sina
//
//  Created by Kent on 16/9/25.
//  Copyright © 2016年 Ming. All rights reserved.
//

import UIKit
import AFNetworking

enum HTTPMethod {
    case GET
    case POST
}

class HMNetworkTools: AFHTTPSessionManager {
    
    static let sharedTool: HMNetworkTools = {
        let st = HMNetworkTools()
        st.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return st
    }()
    
    func request(method: HTTPMethod, urlString: String, parameters: Any?, finished: @escaping (Any?, Error?) -> ()) {
        
        
        let successCallBack = { (task: URLSessionDataTask,responseObject: Any?) -> () in
            finished(responseObject,nil)
        }
        
        let failureCallBack = { (task: URLSessionDataTask?, error: Error) -> () in
            finished(nil,error)
            print(error)
        }
        
        
        if method == .GET {
        
            get(urlString, parameters: parameters, progress: nil, success:successCallBack, failure:failureCallBack)


        } else {
            
            post(urlString, parameters: parameters, progress: nil, success:successCallBack, failure:failureCallBack)
            
        }
        
        
        
        
    }
    
    
}
